package com.jeespring.aop.aspect;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @description PermissionAspect
 * @author 张朝阳
 * @date 2021/4/8 16:47
 */

@Aspect
@Component
public class PermissionAspect {
    /*
    execution表达式定义切入点
     */
    @Pointcut("execution(public * com.jeespring.aop.controller..*.*(..))")
    public void methods(){}

    @Before("methods()")
    public Object doAround(JoinPoint point){
//        try {
//            point.proceed();
//        } catch (Throwable throwable) {
//            return "11";
//        }
        return "12";
    }

    @Around("methods()")
    public Object doAround(ProceedingJoinPoint pjp){
        System.out.println("HH 方法环绕start.....");
        Object[] args = pjp.getArgs();
        System.out.println(JSON.toJSONString(args));
        try {
            // 获取请求参数进行打印
            Signature signature = pjp.getSignature();
            // 类名
            String className = pjp.getTarget().getClass().getName();
            // 方法名
            String methodName = signature.getName();
            List list = new ArrayList();
            System.out.println(className+":"+methodName);
            Object[] methodArgs = pjp.getArgs();
            String methodArg = (String)methodArgs[0];
            System.out.println(JSON.toJSONString(methodArgs));
            Object ret = null;
            // TODO: 此处为自定义验证逻辑，符合条件则继续执行，否则终止方法的执行
            if ( "zhang".equals(methodArg)) {
                // 执行方法
                ret =  pjp.proceed();
                return ret;
            } else {
                return "fail";
            }

        } catch (Throwable e) {
            e.printStackTrace();
            return null;
        }
    }
}
